package com.piotrs10.bsk.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.piotrs10.bsk.model.Samochod;

public class SamochodDao extends BaseDao {
	public ArrayList<Samochod> getAll() {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		ArrayList<Samochod> list = null;
		try {
			statement = connection.prepareStatement("SELECT id, marka, model, cena, rok_produkcji,"+
					"sprzedaz_id, typ_id FROM samochod;");
			ResultSet set = statement.executeQuery();
			list = new ArrayList<Samochod>();
			while(set.next()) {
				list.add(new Samochod(set.getInt(1), set.getString(2), set.getString(3),
						set.getFloat(4), set.getInt(5), set.getInt(6), set.getInt(7)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	public void save(boolean insert, Samochod samochod) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			if(insert) {
				statement = connection.prepareStatement("INSERT INTO Samochod " +
						"(marka, model, cena, rok_produkcji, sprzedaz_id, typ_id) VALUES " +
						"(?, ?, ?, ?, ?, ?)");
			} else {
				statement = connection.prepareStatement(
						"UPDATE Samochod SET marka = ? , model = ?, cena = ?, rok_produkcji = ?, "
						+ "sprzedaz_id = ?, typ_id = ? WHERE id = ?");
			}
			statement.setString(1, samochod.getMarka());
			statement.setString(2, samochod.getModel());
			statement.setFloat(3, samochod.getCena());
			statement.setInt(4, samochod.getRok());
			statement.setInt(5, samochod.getSprzedaz_id());
			statement.setInt(6, samochod.getTyp());
			if(!insert)
				statement.setInt(7, samochod.getId());
			
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(connection != null)
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void delete(Integer id) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement("DELETE FROM Samochod " +
						"WHERE id = ?");
			statement.setInt(1, id);
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(connection != null)
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public Samochod get(int id) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		Samochod samochod = null;
		try {
			statement = connection.prepareStatement("SELECT marka, model, " +
					"cena, rok_produkcji, sprzedaz_id, typ_id FROM samochod WHERE id = ?;");
			statement.setInt(1, id);
			ResultSet set  = statement.executeQuery();
			set.next();
			samochod = new Samochod(id, set.getString(1), set.getString(2), set.getFloat(3),
					set.getInt(4), set.getInt(5), set.getInt(6));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return samochod;
	}
}
